//
//  FileExist.swift
//  WriteToLocal
//
//  Created by 郑东喜 on 2017/3/4.
//  Copyright © 2017年 郑东喜. All rights reserved.
//  文件管理器

import UIKit

class FileExist: NSObject {
    ///创建文件夹
    class func createFolder(name:String,baseUrl:NSURL) {
        let manager = FileManager.default
        let folder = baseUrl.appendingPathComponent(name, isDirectory: true)
        print("文件夹: \(folder)")
        let exist = manager.fileExists(atPath: folder!.path)
        if !exist {
            try! manager.createDirectory(at: folder!, withIntermediateDirectories: true,
                                         attributes: nil)
        }
    }
    
    /// 删除文件
    class func delFile(str : String) -> Void {
        let fileManager = FileManager.default
        let homeDirectory = NSHomeDirectory()
        let srcUrl = homeDirectory + str
        try! fileManager.removeItem(atPath: srcUrl)
    }
    
    /// 存在文件夹
    class func fileIsExist(filePathStr : String) -> Bool {
        let fileManager = FileManager.default
        let filePath:String = NSHomeDirectory() + filePathStr
        let exist = fileManager.fileExists(atPath: filePath)
        
        return exist
    }
}

// MARK:- 颜色扩展
extension UIColor {
    
    /**
     快速构建rgb颜色
     
     - parameter r: r
     - parameter g: g
     - parameter b: b
     
     - returns: 返回rgb颜色对象，alpha默认1
     */
    class func colorWithRGB(_ r: CGFloat, g: CGFloat, b: CGFloat) -> UIColor
    {
        return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
    }
    
    /**
     生成随机颜色
     
     - returns: 返回随机色
     */
    class func randomColor() -> UIColor
    {
        let r = CGFloat(arc4random_uniform(256))
        let g = CGFloat(arc4random_uniform(256))
        let b = CGFloat(arc4random_uniform(256))
        return UIColor.colorWithRGB(r, g: g, b: b)
    }
    
}
